
// <copyright file="BoundedEnumerable.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The bounded enumerable.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Collections.Generic
{
    using System.Collections;
    using System.Collections.Generic;

    /// <summary>
    /// The bounded enumerable.
    /// </summary>
    /// <typeparam name="T">
    /// </typeparam>
    public class BoundedEnumerable<T> : IEnumerable<T>
        where T : class
    {
        #region Fields

        /// <summary>
        /// The end index
        /// </summary>
        private readonly int endIndex;

        /// <summary>
        /// The start index
        /// </summary>
        private readonly int startIndex;

        /// <summary>
        /// The store.
        /// </summary>
        private readonly IDataStructureAdapter<T> store;

        #endregion Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="BoundedEnumerable{T}"/> class.
        /// </summary>
        /// <param name="store">
        /// The store.
        /// </param>
        /// <param name="startIndex">
        /// The start index.
        /// </param>
        /// <param name="endIndex">
        /// The end index.
        /// </param>
        public BoundedEnumerable(IDataStructureAdapter<T> store, int startIndex, int endIndex)
        {
            this.store = store;
            this.startIndex = startIndex;
            this.endIndex = endIndex;
        }

        #endregion Constructors and Finalizers

        #region Public Methods and Operators

        /// <summary>
        /// The get enumerator.
        /// </summary>
        /// <returns>
        /// The <see cref="IEnumerator"/>.
        /// </returns>
        public virtual IEnumerator<T> GetEnumerator()
        {
            return this.GetEnumeratorCore();
        }

        #endregion Public Methods and Operators

        #region Explicit Interface Methods

        /// <summary>
        /// The get enumerator.
        /// </summary>
        /// <returns>
        /// The <see cref="IEnumerator"/>.
        /// </returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumeratorCore();
        }

        #endregion Explicit Interface Methods

        #region Methods

        /// <summary>
        /// The get enumerator.
        /// </summary>
        /// <returns>
        /// The <see cref="IEnumerator"/>.
        /// </returns>
        protected virtual IEnumerator<T> GetEnumeratorCore()
        {
            return new DataStructureEnumerator<T>(this.store, this.startIndex, this.endIndex);
        }

        #endregion Methods
    }
}